import { Navigate } from "react-router-dom";

// 定义路由守卫高阶组件
export const withAuth = (Component) => {
    return (props) => {
        const noLogin = ['/login'];
        const pathname = window.location.pathname;
        try {
            const accountId = sessionStorage.getItem('accountid');
            if (noLogin.includes(pathname) || accountId) {
                return <Component {...props} />;
            }
        } catch (error) {
            console.error('Error accessing sessionStorage:', error);
        }
        return <Navigate to="/login" replace />;
    };
};